Skip to content

fix: handle heredocs with trailing code#25

Merged
jeanpaulsio merged 1 commit into
mainfrom
feat/heredoc-multitoken-rework
Mar 27, 2026
Merged

fix: handle heredocs with trailing code#25
jeanpaulsio merged 1 commit into
mainfrom
feat/heredoc-multitoken-rework

Conversation

@jeanpaulsio

Copy link
Copy Markdown
Owner

Summary

  • Heredocs with trailing code on the opener line now parse correctly: foo(<<~SQL), <<~HEREDOC.strip
  • Uses a hybrid approach: full token when no trailing code (preserves body highlighting), opener-only token when trailing code exists (lets the parser handle ), .method, etc.)
  • All 105 existing tests pass, no regressions

What changed

Two files modified (src/tokens.ts, src/syntax.grammar):

  • tryMatchHeredoc now detects trailing code after the opener and returns either the full heredoc length or just the opener length
  • Grammar comment updated to document the hybrid approach

Test plan

  • All 105 existing tests pass
  • foo(<<~SQL)\n SELECT 1\nSQL — parses as MethodCall with Heredoc arg, no error nodes
  • <<~HEREDOC.strip\n hello\nHEREDOC — parses as MethodCall chain, no error nodes
  • a << b — still parsed as left shift, not heredoc
  • def foo\n x = <<~SQL\n SELECT 1\n SQL\n x\nend — heredoc inside method body works

The heredoc tokenizer now uses a hybrid approach:
- No trailing code: full token spanning opener + body + delimiter (existing behavior)
- Trailing code after opener: opener-only token so the parser handles ), .method, etc.

This fixes parse errors for patterns like foo(<<~SQL) and <<~HEREDOC.strip
while keeping all 105 existing tests passing.
@jeanpaulsio jeanpaulsio merged commit 8a449ba into main Mar 27, 2026
1 check passed
@jeanpaulsio jeanpaulsio deleted the feat/heredoc-multitoken-rework branch March 27, 2026 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant